#include "RandomTexture.h"

RandomTexture::RandomTexture(ID3D10Device * device)
{
	m_d3dDevice=device;

	D3D10_TEXTURE3D_DESC desc;
	desc.Width=64;
	desc.Height=64;
	desc.Depth=64;
	desc.MipLevels=1;
	desc.Format=DXGI_FORMAT_R32_FLOAT;
	desc.Usage=D3D10_USAGE_DYNAMIC;
	desc.BindFlags=D3D10_BIND_SHADER_RESOURCE;
	desc.CPUAccessFlags=D3D10_CPU_ACCESS_WRITE;
	desc.MiscFlags=0;

	m_d3dDevice->CreateTexture3D(&desc, NULL, &m_Texture);


	D3D10_MAPPED_TEXTURE3D mappedTex;
	m_Texture->Map(D3D10CalcSubresource(0,0,1),D3D10_MAP_WRITE_DISCARD, 0,&mappedTex);


	float * randomData=(float*)mappedTex.pData;
	for(int i=0; i<desc.Width; i++)
	{
		for(int j=0; j<desc.Height; j++)
		{
			for(int k=0; k<desc.Depth; k++)
			{
				randomData[i*desc.Width*desc.Depth+j*desc.Width+k]=(float)(rand()%100)/100;
			}
		}
	}

	m_Texture->Unmap((D3D10CalcSubresource(0,0,1)));

	D3D10_SHADER_RESOURCE_VIEW_DESC srDesc;
	srDesc.Format = desc.Format;
	srDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE3D;
	srDesc.Texture2D.MostDetailedMip = 0;
	srDesc.Texture2D.MipLevels = 1;

	m_d3dDevice->CreateShaderResourceView(m_Texture,&srDesc,&m_TextureView);

}

RandomTexture::~RandomTexture()
{
	if(m_Texture)
	{
		m_Texture->Release();
		m_Texture=NULL;
	}

	if(m_TextureView)
	{
		m_TextureView->Release();
		m_TextureView=NULL;
	}
	


}

ID3D10ShaderResourceView* RandomTexture::GetRandomTexture()
{
	return m_TextureView;
}